﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class OvertimeEdit : System.Web.UI.Page
    {
        protected ReqOvertime reqOvertime;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (formNo != null)
                {
                    reqOvertime = ctx.ReqOvertimes.Single(r => r.FormNo == formNo);
                    Employee employee = ctx.Employees.Single(r => r.Id == reqOvertime.EmpId);
                    empName = employee.Name;
                    if(reqOvertime.PreStartTime.Hour<=7||reqOvertime.PreStartTime.Hour==8&&reqOvertime.PreStartTime.Minute<30)
                    {
                        PreStartTime.Value = reqOvertime.PreStartTime.ToString("yyyy-MM-dd 08:30:00");
                    }
                    else
                    {
                        PreStartTime.Value = reqOvertime.PreStartTime.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    PreEndTime.Value = reqOvertime.PreEndTime.ToString("yyyy-MM-dd HH:mm:ss");
                    Cause.Value = reqOvertime.Cause;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    empName = ebi.Name;
                    reqOvertime = new ReqOvertime();
                    reqOvertime.ReqDate = DateTime.Now;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(formNo) && formNo != "undefined")
            {
                reqOvertime = ctx.ReqOvertimes.Single(r => r.FormNo == formNo);
                //删除之前的签核数据
                var list = from r in ctx.SignFlows where r.FormNo == formNo select r;
                ctx.SignFlows.DeleteAllOnSubmit(list);
            }
            else
            {
                reqOvertime = new ReqOvertime();
                string maxFormNo = ctx.ReqOvertimes.Max(r => r.FormNo);
                formNo = "Form_Over_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Over_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                reqOvertime.FormNo = formNo;
                reqOvertime.ReqDate = DateTime.Now;
                reqOvertime.EmpId = ebi.Id;
                ctx.ReqOvertimes.InsertOnSubmit(reqOvertime);
            }
            reqOvertime.PreStartTime = DateTime.Parse(PreStartTime.Value);
            reqOvertime.PreEndTime = DateTime.Parse(PreEndTime.Value);
            reqOvertime.Cause = Cause.Value;
            reqOvertime.StatusId = isSubmit;
            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                fs.SetSignFlow(reqOvertime.FormNo, ebi);
            }
            Response.Write("<script>alert('修改成功');window.returnValue='" + reqOvertime.FormNo + "';window.close();</script>");
        }
    }
}